﻿#include "stdafx.h"

#include "svgeditor.h"
#include "ui/ui_mainwindow.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QScrollArea>
#include <QScrollBar>

#include "svgreader.h"

SvgEditor::SvgEditor(QWidget* parent)
	: QMainWindow(parent)
	, m_ui(new Ui::SvgEditor)
{
	m_ui->setupUi(this);
	_InitUi();
	m_ui->svgWidget->Init();
}

SvgEditor::~SvgEditor()
{
	delete m_ui;
}

void SvgEditor::ChangeOperation(SvgOperationType op) const
{
	switch (op)
	{
	case SvgOperationType::AddLine:
		m_ui->action_addLine->setChecked(true);
		break;
	case SvgOperationType::AddRect:
		m_ui->action_addRect->setChecked(true);
		break;
	case SvgOperationType::AddEllipse:
		m_ui->action_addEllipse->setChecked(true);
		break;
	case SvgOperationType::AddFreeLine:
		m_ui->action_addFreeLine->setChecked(true);
		break;
	case SvgOperationType::AddText:
		m_ui->action_addText->setChecked(true);
		break;
	case SvgOperationType::AddIsoscelesTriangle:
		m_ui->action_addIsoscelesTriangle->setChecked(true);
		break;
	case SvgOperationType::AddPentagon:
		m_ui->action_addPentagon->setChecked(true);
		break;
	case SvgOperationType::AddHexagon:
		m_ui->action_addHexagon->setChecked(true);
		break;
	case SvgOperationType::SelectShape:
		m_ui->action_select->setChecked(true);
		break;
	default:
		ASSERT(false);
		break;
	}
}

void SvgEditor::_OpenSvg()
{
	const QString path = QFileDialog::getOpenFileName(this, tr("choose save path"), ".", tr("Svg Files(*.svg)"));
	if (!path.isEmpty())
		m_ui->svgWidget->OpenSvg(path);
}

void SvgEditor::_NewSvg()
{
	const QMessageBox::StandardButton sure = QMessageBox::question(this,
		tr("Sure Save"),
		tr("new svg image will make canvas empty"));
	if (sure == QMessageBox::StandardButton::Yes)
		m_ui->svgWidget->NewSvg();
}

void SvgEditor::_SaveSvg()
{
	const QString path = QFileDialog::getSaveFileName(this, tr("choose save path"), ".", tr("Svg Files(*.svg)"));
	if (!path.isEmpty())
		m_ui->svgWidget->SaveSvg(path);
}

void SvgEditor::_ChangeShapeToLine() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddLine);
}

void SvgEditor::_ChangeShapeToRect() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddRect);
}

void SvgEditor::_ChangeShapeToEllipse() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddEllipse);
}

void SvgEditor::_ChangeShapeToPentagon() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddPentagon);
}

void SvgEditor::_ChangeShapeToHexagon() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddHexagon);
}

void SvgEditor::_ChangeShapeToIsoscelesTriangle() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddIsoscelesTriangle);
}

void SvgEditor::_ChangeShapeToText() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddText);
}

void SvgEditor::_ChangeShapeToFreeLine() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::AddFreeLine);
}

void SvgEditor::_ChangeOperationToSelect() const
{
	m_ui->svgWidget->ChangeOperation(SvgOperationType::SelectShape);
}

void SvgEditor::_InitUi()
{
	auto group = new QActionGroup(this);
	group->addAction(m_ui->action_addEllipse);
	group->addAction(m_ui->action_addLine);
	group->addAction(m_ui->action_addRect);
	group->addAction(m_ui->action_addFreeLine);
	group->addAction(m_ui->action_addText);
	group->addAction(m_ui->action_addIsoscelesTriangle);
	group->addAction(m_ui->action_addPentagon);
	group->addAction(m_ui->action_addHexagon);
	group->addAction(m_ui->action_select);

	connect(m_ui->svgWidget, &SvgWidget::ColorChanged, m_ui->svgBottomToolBar->palette, &SvgPalette::ChangeColor);
	connect(m_ui->svgBottomToolBar->palette, &SvgPalette::ColorChanged, m_ui->svgWidget, &SvgWidget::ChangeColor);

	connect(m_ui->svgWidget, &SvgWidget::SelectedShapesUpdated, m_ui->svgExtraInfoWidget, &SvgExtraInfoWidget::ShowShapesData);

	connect(m_ui->svgExtraInfoWidget->canvasInfoWidget, &SvgCanvasInfoWidget::CanvasSizeChanged, m_ui->svgWidget, &SvgWidget::ChangeCanvasSize);
	connect(m_ui->svgExtraInfoWidget->canvasInfoWidget, &SvgCanvasInfoWidget::CanvasBackgroundChanged, m_ui->svgWidget, &SvgWidget::ChangeCanvasBackground);
	connect(m_ui->svgExtraInfoWidget->lineInfoWidget, &SvgLineInfoWidget::ShapeStrokeWidthChanged, m_ui->svgWidget, &SvgWidget::ChangeSelectedStrokeWidth);
	connect(m_ui->svgExtraInfoWidget->lineInfoWidget, &SvgLineInfoWidget::LinePosChanged, m_ui->svgWidget, &SvgWidget::ChangeSelectedLinePos);

	connect(m_ui->scrollArea->verticalScrollBar(), &QScrollBar::valueChanged, [this](int){m_ui->svgWidget->ViewPortChanged();});
	connect(m_ui->scrollArea->horizontalScrollBar(), &QScrollBar::valueChanged, [this](int){m_ui->svgWidget->ViewPortChanged();});
}
